﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.ServiceModel;

namespace DMSWcfServices.xmls
{
    public partial class getXMLSurtidosUNL : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Xml que retorna los eventos de la pagina
            Response.Expires = 0;
            Response.ExpiresAbsolute = DateTime.MinValue;
            Response.AddHeader("PRAGMA", "NO-CACHE");
            Response.AddHeader("Expires", "-1");
            Response.CacheControl = "no-cache";
            Response.ContentType = "text/xml";

            WSHttpBinding binding = new WSHttpBinding();
            binding.CloseTimeout = new TimeSpan(0, 1, 0);
            binding.OpenTimeout = new TimeSpan(0, 1, 0);
            binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
            binding.SendTimeout = new TimeSpan(0, 2, 0);
            binding.Security.Mode = SecurityMode.None;

            EndpointAddress address = new EndpointAddress(Request.QueryString["PSDS"].ToString());

            DMSServices.DMSServicesClient service = new DMSServices.DMSServicesClient(binding, address);
            List<Dtl.Surtidos> listSurtidos = null;
            try
            {
                //+ "&PSDS=" + Properties.Settings.Default.Server
                listSurtidos = service.getSurtidosReport(Request.QueryString["cdPais"].ToString(), Request.QueryString["ambient"].ToString());
            }
            catch (Exception ee)
            {
                listSurtidos = new List<Dtl.Surtidos>();
            }

            XmlTextWriter cdRSS = new XmlTextWriter(this.Response.OutputStream, System.Text.Encoding.UTF8);
            cdRSS.WriteStartDocument();
            cdRSS.WriteStartElement("surtidos");

            if (listSurtidos.Count > 0)
            {            
                foreach (Dtl.Surtidos oi in listSurtidos)
                {
                    cdRSS.WriteStartElement("surtido");
                    cdRSS.WriteAttributeString("dsCountry", oi.dsCountry);
                    cdRSS.WriteAttributeString("cdSKU", oi.cdSKU.ToString().Trim());
                    cdRSS.WriteAttributeString("dsProduct", oi.dsProduct.ToString().Trim());
                    cdRSS.WriteEndElement();
                }
            }            
            cdRSS.WriteEndDocument();
            cdRSS.Flush();
            cdRSS.Close();
            this.Response.End();

        }                      
    }
}
